package Offer.offer;

/**
 * Created by 雷文康 on 2018/1/6.
 */
public class Test42a翻转单词顺序 {
    public static void main(String[] args) {
        String str="abc ef db ef ca";
        reverseStr(str);
    }

    public static void reverseStr(String str){
        if (str == null || str.length() == 0){
            return;
        }
        char[] ch = str.toCharArray();
        int start = 0;
        int end = 0;
        while (true) {
            if (ch[start] == ' '){
                start++;
                end++;
            } else if (ch[end] == ' '){
                reverse(ch, start, end - 1);
                end++;
                start = end;
            } else if (end == ch.length - 1) {
                reverse(ch, start, end);
                break;
            } else {
                end++;
            }
        }
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
    }

    private static void reverse(char[] ch, int start, int end){
        while (start < end){
            char temp = ch[start];
            ch[start] = ch[end];
            ch[end] = temp;
            start++;
            end--;
        }
    }
}
